package core.manager;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import tool.Complex;
import tool.Pair;
import tool.RandomSignal;
import gui.MainForm;
import core.FilterFactory;
import core.filter.BaseFilter;
import core.filter.type.FilterTypes;

public class FilterManager {

	private static FilterManager instance;
	
	public void calculateFilter(FilterTypes type, double suppressionP, double suppressionR,
			double frequencyP, double frequencyR){
		BaseFilter filter = FilterFactory.getFilterInstance(type, suppressionP, suppressionR,
				frequencyP, frequencyR);
		filter.setFrequencyP(frequencyP);
		filter.setFrequencyR(frequencyR);
		filter.setSuppressionP(suppressionP);
		filter.setSuppressionR(suppressionR);
		List<Double> list = RandomSignal.getSignal();
//		
		List<Pair> resultList = filter.getTransmittance(list);
		List<Complex> polesList = filter.getPolesList();
		MainForm form = MainForm.getInstance();
		Collections.sort(resultList, new Compar<Pair>());
		form.setList(resultList);
		form.setScatterList(polesList);
		form.createChart();
		form.createPolesScatter();
	}
	
	public void startFilter(){
		MainForm form = MainForm.getInstance();
		form.drawChart();
	}
	
	@SuppressWarnings("hiding")
	private class Compar<T> implements Comparator<T> {

		@Override
		public int compare(T arg0, T arg1) {
			if(arg0 instanceof Pair && arg1 instanceof Pair) {
				Pair a = (Pair)arg0;
				Pair b = (Pair)arg1;
				if(b.getX() > a.getX())
					return 1;
				else if(b.getX() < a.getX())
					return -1;
			}
			return 0;
		}
		
	}
}
